<?php 
class View_out extends View {
    
    const SCRIPT_BOX = 'SCRIPT_BOX';
    const STYLE_BOX = 'STYLE_BOX';
    
    private $title = '';
    private $keywords = '';
    private $description = '';
    
    private $pre_jses = array();
    private $jses = array();
    private $scripts = array();
    
    private $csses = array();
    private $styles = array();
    
    
    protected $layout;
    
    public function set_layout($layout) {
        if ($layout instanceof View) {
            $this->layout = $layout;
        }
    }
    
    public function add_title($title) {
        $title = (string) $title;
        if (empty($this->title)) {
            $this->title = $title;
        } else {
            $this->title .= ' - ' . $title;
        }
    }
    
    
    public function add_keywords($keywords) {
        $keywords = (string) $keywords;
        if (empty($this->keywords)) {
            $this->keywords = $keywords;
        } else {
            $this->keywords .= ', ' . $keywords;
        }
    }
    
    
    
    public function add_description($description) {
        $description = (string) $description;
        if (empty($this->description)) {
            $this->description = $description;
        } else {
            $this->description .= '; ' . $description;
        }
    }
    
    
    public function add_js($js) {
        $this->jses[] = $js;
    }
    
    
    public function add_pre_js($js) {
        $this->pre_jses[] = $js;
    }
    
    
    public function add_script($script_view) {
        $this->add_view($script_view, self::SCRIPT_BOX);
    }
    
    
    public function add_css($css) {
        $this->csses[] = $css;
    }
    

    
    
    public function add_style($style_view) {
        $this->add_view($style_view, self::STYLE_BOX);
    }
    
    
    
    public function display_jses() {
        if ($this->jses) {
            foreach ($this->jses as $js) {
                echo "<script type=\"text/javascript\" src=\"{$js}\"></script>";
            }
        }
    }
    
    
    public function display_pre_jses() {
        if ($this->pre_jses) {
            foreach ($this->jses as $js) {
                echo "<script type=\"text/javascript\" src=\"{$js}\"></script>";
            }
        }
    }
    
    
    public function display_scripts() {
        if ( ! $this->scripts) {
            return;
        }
        echo '<script tyle="text/javascript">';
        foreach ($this->scripts as $script) {
            $script->view();
        }
        echo '</script>';
    }
    
    
    public function display_csses() {
        if ( ! $this->csses) {
            return;
        }
        
        foreach ($this->csses as $css) {
            echo "<link rel=\"stylesheet\" href=\"{$css}\" type=\"text/css\" />";
        }
    }
    
    
    public function display_html_title() {
        echo "<title>{$this->title}</title>";
    }
    
    
    public function display_html_keywords() {
        echo "<meta name=\"keywords\" content=\"{$this->keywords}\" />";
    }
    
    
    public function display_html_description() {
        echo "<meta name=\"description\" content=\"{$this->description}\" />";
    }
    
    public function display_styles() {
        if ( ! $this->styles) {
            return;
        }
        
        echo '<style>';
        foreach ($this->styles as $style) {
            $style->view();
        }
        echo '</style>';
    }
}
?>